package com.iyao_app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.iyao_app.R;
import com.iyao_app.im.adapter.AttentionMyInterestAdapter;
import com.iyao_app.entry.MyInterestBean;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.User;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.mvp.other.OtherHomeActivity;
import com.iyao_app.util.Contants;
import com.iyao_app.util.DbUtil;
import com.iyaowoqu.base_library.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xue on 2017/11/24.
 * 我关注的
 */

public class MyInterestFragment extends Fragment implements AttentionMyInterestAdapter.ONCLICK, OnItemClickListener {

    int page = 1, pageSize = 1000, type = 1;
    public List<MyInterestBean> modelList = new ArrayList<>();
    @BindView(R.id.my_interest_hint)
    TextView mMyInterestHint;
    @BindView(R.id.my_interest_no)
    TextView mMyInterestNo;
    @BindView(R.id.recyclerview_friend)
    RecyclerView mRecyclerviewFriend;
    Unbinder unbinder;
    @BindView(R.id.iv_abnormal)
    ImageView mIvAbnormal;
    private RecyclerView recyclerView;
    private AttentionMyInterestAdapter attentionFriendAdapter;
    private String friendId;
    private PopupWindow mPopWindow;
    private String id;
    private String nickname;
    private String favicon;
    private int sex;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_myinterest, null);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_friend);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getString(Contants.OTHER_FIELD);
        }
        if (TextUtils.isEmpty(id)) {
            User user = DbUtil.getUser();
            id = user.id;
            nickname = user.nickname;
            favicon = user.avatarUrl;
            sex = user.sex;
        }
        getData();

    }

    private void getData() {
        NetUtils.getInstance().getFriends(id,nickname,favicon,sex,page, pageSize, type, new NetCallBack() {
            @Override
            public void onSuccess(String message, final int code, String response, final ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model != null) {
                        modelList = (List<MyInterestBean>) model.getModelList();
                        if (modelList.size() != Contants.ZERO) {
                            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                            attentionFriendAdapter = new AttentionMyInterestAdapter(getContext(), recyclerView, modelList, MyInterestFragment.this, mListener);
                            recyclerView.setAdapter(attentionFriendAdapter);
                        } else {
                            mMyInterestHint.setVisibility(View.VISIBLE);
                            mMyInterestNo.setVisibility(View.VISIBLE);
                            mIvAbnormal.setVisibility(View.VISIBLE);
                            mRecyclerviewFriend.setVisibility(View.GONE);
                        }
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, MyInterestBean.class);

    }

    @Override
    public void onclick(int position) {
        Intent intent = new Intent(getContext(), OtherHomeActivity.class);
        intent.putExtra(Contants.TYPE, String.valueOf(modelList.get(position).getFriendId()));
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    /**
     * 实现类，响应按钮点击事件
     */
    private AttentionMyInterestAdapter.MyClickListener mListener = new AttentionMyInterestAdapter.MyClickListener() {
        @Override
        public void myOnClick(final int position, View v) {

            friendId = String.valueOf(modelList.get(position).getFriendId());

            //设置contentView
            View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.item_popupwindow, null);
            mPopWindow = new PopupWindow(contentView,
                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            mPopWindow.setContentView(contentView);
            //设置各个控件的点击响应
            TextView tv2 = (TextView) contentView.findViewById(R.id.pop_financial);
            TextView tv3 = (TextView) contentView.findViewById(R.id.pop_manage);
            tv2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    NetUtils.getInstance().unfollowFriend(friendId, new NetCallBack() {
                        @Override
                        public void onSuccess(String message, final int code, String response, final ResultModel model) {
                            if (code == Contants.SUCCESS_CODE) {
                                modelList.remove(position);
                                Log.e("----", modelList.toString());

                                if (position == Contants.ZERO) {
                                    mMyInterestHint.setVisibility(View.VISIBLE);
                                    mMyInterestNo.setVisibility(View.VISIBLE);
                                    mIvAbnormal.setVisibility(View.VISIBLE);
                                    mRecyclerviewFriend.setVisibility(View.GONE);
                                }
                                attentionFriendAdapter.notifyDataSetChanged();
                            }
                        }

                        @Override
                        public void onFail(String message, int code) {

                        }
                    }, null);
                    mPopWindow.dismiss();
                }
            });
            tv3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPopWindow.dismiss();
                }
            });
            //显示PopupWindow
            View rootview = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_myinterest, null);
            mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);

        }

    };

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden){
            getData();
        }
    }
}

